package com.chenchao.net;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;

/**
 * @encoding : UTF-8
 * @Time : 2020/11/26 10:21
 * @Author : Chen Chao
 * @File : javaSE.com.chenchao.net.Test11A.java
 * @Software : IntelliJ IDEA
 * @since : 1.8
 * TCP客户端
 */
public class Test11A {
    public static void main(String[] args) throws IOException {
        /*新建socket对象,先建立连接，如果找不到就会报错*/
        Socket socket = new Socket("127.0.0.1",10000);
        /*输出流写入*/
        OutputStream os = socket.getOutputStream();
        os.write("呼叫服务器，\n收到请回答".getBytes());
        /*关闭输出流，不然服务器会死等（阻塞）*/
        socket.shutdownOutput();

        /*输入流写入*/
        BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        String str ;
        while ((str=br.readLine())!=null){
            System.out.println(str);
        }
        /*关闭资源*/
        os.close();
        br.close();
        socket.close();
    }
}
